package com.example.familylibrary.service.impl;

import com.example.familylibrary.dto.CategoryDTO;
import com.example.familylibrary.entity.CategoryEntity;
import com.example.familylibrary.repository.CategoryRepository;
import com.example.familylibrary.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryRepository categoryRepository;

    @Override
    public CategoryDTO createCategory(CategoryDTO categoryDTO) {
        CategoryEntity categoryEntity = new CategoryEntity();
        BeanUtils.copyProperties(categoryDTO, categoryEntity);
        
        CategoryEntity savedCategory = categoryRepository.save(categoryEntity);
        
        CategoryDTO result = new CategoryDTO();
        BeanUtils.copyProperties(savedCategory, result);
        return result;
    }

    @Override
    public CategoryDTO getCategoryById(Long id) {
        CategoryEntity categoryEntity = categoryRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("分类不存在"));
        
        CategoryDTO result = new CategoryDTO();
        BeanUtils.copyProperties(categoryEntity, result);
        return result;
    }

    @Override
    public List<CategoryDTO> getAllCategories() {
        return categoryRepository.findAll().stream().map(categoryEntity -> {
            CategoryDTO categoryDTO = new CategoryDTO();
            BeanUtils.copyProperties(categoryEntity, categoryDTO);
            return categoryDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<CategoryDTO> getCategoriesByParentId(Long parentId) {
        return categoryRepository.findByParentIdOrderBySortAsc(parentId).stream().map(categoryEntity -> {
            CategoryDTO categoryDTO = new CategoryDTO();
            BeanUtils.copyProperties(categoryEntity, categoryDTO);
            return categoryDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public CategoryDTO updateCategory(Long id, CategoryDTO categoryDTO) {
        CategoryEntity categoryEntity = categoryRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("分类不存在"));
        
        categoryEntity.setName(categoryDTO.getName());
        categoryEntity.setParentId(categoryDTO.getParentId());
        categoryEntity.setSort(categoryDTO.getSort());
        
        CategoryEntity updatedCategory = categoryRepository.save(categoryEntity);
        
        CategoryDTO result = new CategoryDTO();
        BeanUtils.copyProperties(updatedCategory, result);
        return result;
    }

    @Override
    public void deleteCategory(Long id) {
        if (!categoryRepository.existsById(id)) {
            throw new RuntimeException("分类不存在");
        }
        categoryRepository.deleteById(id);
    }
}